; -*- MIDAS -*-
;;; Copyright (c) 1999 Massachusetts Institute of Technology
;;;
;;; This program is free software; you can redistribute it and/or
;;; modify it under the terms of the GNU General Public License as
;;; published by the Free Software Foundation; either version 3 of the
;;; License, or (at your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program; if not, see https://gnu.org/licenses or
;;; write to:
;;;  Free Software Foundatiom, Inc.
;;;  51 Franklin St, Fifth Floor
;;;  Boston, MA 02110-1301
;;;  USA


	TITLE DMPCPY - COPY DUMPS FROM SWAP AREA TO FILE AREA

A=1
B=2
C=3
D=4
E=5
T=6
TT=7
P=17

CHDIRI=10
CHDSKI=11
CHDSKO=12

.INSRT SYSENG;FSDEFS >

PDL:	-20,,.
	BLOCK 20

;DIRECTORIES WHERE STUFF MIGHT GET DUMPED OUT OF TIMESHARING
DIRLST:	SIXBIT/./
	SIXBIT/CRASH/
	SIXBIT/CRASH2/
NDIRS==.-DIRLST

DIR:	BLOCK 2000
BUF:	BLOCK 2000

USRVAR:	SIXBIT /OPTION/	? TLO %OPINT\%OPOPC
	SIXBIT /MASK/	? MOVE [%PIIOC\%PIPDL]
LUSRVAR==:.-USRVAR

GO:	.CLOSE 1,		;RUNS AS DAEMON
	MOVE P,PDL
	MOVE TT,[-LUSRVAR,,USRVAR]
	.CALL [	SETZ ? SIXBIT /USRVAR/
		MOVEI %JSELF
		SETZ TT ]
	 .LOSE %LSSYS
CHKTIM:	.CALL [	SETZ ? SIXBIT /RQDATE/	; don't frob with files
		SETZM A ]	; if system doesn't know the time
	 .LOSE %LSSYS
	AOJN A,KNOTIM
	MOVEI A,30.*15.		; 15 seconds
	.SLEEP A,
	JRST CHKTIM

KNOTIM:	MOVSI E,-NDIRS
CHKDIR:	.CALL [	SETZ ? SIXBIT/OPEN/
		[.BII,,CHDIRI]
		[SIXBIT/DSK/]
		[SIXBIT/.FILE./]
		[SIXBIT/(DIR)/]
		SETZ DIRLST(E)]
	 JRST NXTDIR		; Hey, there might not -be- such a directory!
	MOVE TT,[-2000,,DIR]
	.IOT CHDIRI,TT
	.CLOSE CHDIRI,
	MOVE D,UDNAMP+DIR
CHKFIL:	CAIL D,2000
	 JRST NXTDIR
	MOVE TT,UNRNDM+DIR(D)
	SKIPGE UNDATE+DIR(D)
	 TLNE TT,UNDUMP\UNIGFL\UNREAP\UNLINK	; Don't screw with files
	  JRST NXTFIL				; you don't understand.
	;HERE WE HAVE FOUND A FILE WRITTEN OUT OF TIMESHARING
	;COPY IT SO AS TO (1) SET THE FILE DATE AND (2) GET IT OUT
	; OF THE SWAPPING AREA.  NTS FILES ARE WRITTEN IN THE SWAPPING
	; AREA TO DECREASE THE PROBABILITY OF CLOBBERING SOME OTHER FILE.
	.CALL [	SETZ ? SIXBIT/OPEN/
		MOVES TT
		[.BII,,CHDSKI]
		[SIXBIT/DSK/]
		UNFN1+DIR(D)
		UNFN2+DIR(D)
		SETZ DIRLST(E) ]
	 JSP T,[ CAIE TT,%ENSFL		; perhaps a hacker deleted it
		  CAIN TT,%ENAPK	; perhaps that pack had to go offline
		   JRST NXTFIL
		 JRST LOSE ]
	.CALL [	SETZ ? SIXBIT/OPEN/
		MOVES TT
		[.BIO,,CHDSKO]
		[SIXBIT/DSK/]
		[SIXBIT/_DMPCP/]
		[SIXBIT/OUTPUT/]
		SETZ DIRLST(E) ]
	 JSP T,[ CAIE TT,%EFLDR
		  JRST LOSE
		 MOVEI TT,30.*60.*5	; 5 minutes
		 .SLEEP TT,
		 JRST .-1 ]
CPYFIL:	MOVE TT,[-2000,,BUF]
	.IOT CHDSKI,TT
	MOVEI T,-BUF(TT)
	JUMPE T,ENDFIL
	MOVNS T
	MOVSS T
	HRRI T,BUF
	.IOT CHDSKO,T
	JRST CPYFIL

ENDFIL:	.CLOSE CHDSKI,
	.CALL [	SETZ ? 'RENMWO
		MOVEI CHDSKO
		UNFN1+DIR(D)
		SETZ UNFN2+DIR(D) ]
	 .LOSE %LSSYS
	.CLOSE CHDSKO,
NXTFIL:	ADDI D,LUNBLK
	JRST CHKFIL

NXTDIR:	AOBJN E,CHKDIR
	.LOGOUT 1,
	.VALUE

; JSP T,LOSE is like .LOSE %LSFIL(TT)
LOSE:	.CALL [	SETZ ? SIXBIT /LOSE/
		MOVEI %LSFIL(TT)
		SETZI -2(T) ]
	 .LOSE %LSSYS

TSINT:
LOC 42
	-LTSINT,,TSINT
LOC TSINT
	P
	%PIIOC ? 0 ? %PIIOC ? 0 ? IOCINT
LTSINT==:.-TSINT

; If there is a disk or directory full problem, wait for someone to notice.
IOCINT:	PUSH P,T
	.SUSET [.RBCHN,,T]
	CAIE T,CHDSKO
	 .LOSE
	.STATUS CHDSKO,T
	LDB T,[330500,,T]
	CAIE T,11		; DEVICE FULL
	 CAIN T,14		; DIRECTORY FULL
	  JRST WAIT
	.VALUE

WAIT:	MOVEI T,30.*60.*5	; 5 minutes
	.SLEEP T,
	POP P,T
	.CALL [ SETZ ? SIXBIT /DISMIS/
		SETZ P ]
	 .LOSE %LSSYS

END GO
